﻿using System;
using SharpCompress.Archives;
using SharpCompress.Archives.Zip;
using SharpCompress.Common;
using SharpCompress.Writers;

namespace ComicCrop
{
    public class ArchiveTool
    {
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="targetFile">解压文件路径</param>
        /// <param name="zipDir">解压文件后路径</param>
        public static void Decompression(string targetFile, string zipDir)
        {
            using (var archive = ArchiveFactory.Open(targetFile))
            {
                archive.WriteToDirectory(zipDir, new ExtractionOptions()
                {
                    ExtractFullPath = true,
                    Overwrite = true
                });
            }

            // var archive = ArchiveFactory.Open(targetFile);
            // foreach (var entry in archive.Entries)
            // {
            //     if (!entry.IsDirectory)
            //     {
            //         entry.WriteToDirectory(zipDir, new ExtractionOptions()
            //         {
            //             ExtractFullPath = true,
            //             Overwrite = true
            //         });
            //     }
            // }
        }

        /// <summary>
        /// 压缩文件
        /// </summary>
        /// <param name="targetDir">压缩文件夹路径</param>
        /// <param name="zipFile">压缩文件路径</param>
        public static void Compression(string targetDir, string zipFile)
        {
            using (var archive = ZipArchive.Create())
            {
                WriterOptions writerOptions = new WriterOptions(CompressionType.Deflate) { ArchiveEncoding = new ArchiveEncoding() { Default = System.Text.Encoding.UTF8 } };
                archive.AddAllFromDirectory(targetDir);
                archive.SaveTo(zipFile, writerOptions);
            }
        }

        public static void Compression(string[] targetFiles, string zipFile)
        {
            using (var archive = ZipArchive.Create())
            {
                WriterOptions writerOptions = new WriterOptions(CompressionType.Deflate) { ArchiveEncoding = new ArchiveEncoding() { Default = System.Text.Encoding.UTF8 } };
                foreach (var item in targetFiles)
                {
                    archive.AddEntry(item,zipFile);
                }
                archive.SaveTo(zipFile, writerOptions);
            }
        }
    }
}
